/*
 * @Author: chestnut_647 
 * @Date: 2017-07-21 13:44:29 
 * @Last Modified by: chestnut_647
 * @Last Modified time: 2017-07-21 20:00:35
 */
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide  = require('page/common/nav-side/index.js');
var _mm = require('util/mm.js');
var _user = require('service/user-service.js');
var formData = {
  oldPassword: '',
  newPassword: '',
  againPassword: ''
}

var page = {
  init: function() {
    this.onLoad();
  },
  onLoad: function() {
    // 初始化左侧菜单
    navSide.init({name: 'pass-update'});
    // 加载用户信息
    this.bindEvent();
  },
  bindEvent: function() {
    var _this = this;
    $('.btn-submit').click(function() {
      formData.oldPassword = $.trim($('#originPass').val());
      formData.newPassword = $.trim($('#newPass').val());
      formData.againPassword = $.trim($('#againPass').val());
      var validateRes = _this.validateForm(formData);
      if(validateRes.status) {
        _user.updatePassword({
          passwordOld: formData.oldPassword,
          passwordNew: formData.newPassword
        }, function(res, msg){
          _mm.successTip(msg);
        }, function(errMsg) {
          _mm.errorTip(errMsg);
        })
      }
      else {
        _mm.errorTip(validateRes.msg);
      }
    })
  },
  validateForm: function(userInfo) {
      var result = {
        status: false,
        msg:''
      };
    if(!_mm.validate(formData.oldPassword, 'required')) {
      result.msg = "原始密码不能为空";
      return result;
    }
    if(!_mm.validate(formData.newPassword, 'required')) {
      result.msg = "原始密码不能为空";
      return result;
    }
    if(formData.newPassword.length < 6) {
      result.msg = "密码长度不能少于六位";
      return result;
    }
    if(formData.newPassword !== formData.againPassword) {
      result.msg = "两次输入密码必须一致";
      return result;
    }
 
    // 验证通过
    result.status = true;
    result.msg = "验证通过";
    return result;

  }
  
};
$(function() {
  page.init();
})